package com.rainkaze.IntelliDrawNote.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.rainkaze.IntelliDrawNote.R;
import com.rainkaze.IntelliDrawNote.bean.ChatMessage;
import com.rainkaze.IntelliDrawNote.ui.NoteEditActivity;

import java.util.List;

import io.noties.markwon.Markwon;

public class AIChatAdapter extends RecyclerView.Adapter<AIChatAdapter.ViewHolder> {
    private List<ChatMessage> chatMessages;
    public AIChatAdapter(List<ChatMessage> chatMessages) {
        this.chatMessages = chatMessages;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ChatMessage chatMessage = chatMessages.get(position);

        if (chatMessage.isUserMessage()) {
            holder.userMessageTextView.setVisibility(View.VISIBLE);
            holder.aiMessageTextView.setVisibility(View.GONE);
            holder.userMessageTextView.setText(chatMessage.getMessage());

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.userMessageTextView.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_END);
            holder.userMessageTextView.setLayoutParams(params);

            holder.aiMessageTextView.setLayoutParams(new RelativeLayout.LayoutParams(0, 0));
            // 用户消息长按复制
//            holder.userMessageTextView.setOnLongClickListener(v -> {
//                copyToClipboard(v, chatMessage.getMessage());
//                return true;
//            });
            holder.userMessageTextView.setText(chatMessage.getMessage());
            holder.userMessageTextView.setTextIsSelectable(true);
            holder.userMessageTextView.setOnLongClickListener(v -> {
                new AlertDialog.Builder(v.getContext())
                        .setTitle("保存提问")
                        .setMessage("是否将该提问保存在新建笔记？")
                        .setPositiveButton("确认", (dialog, which) -> {
                            // 执行保存逻辑，比如跳转新建笔记页面并传入内容
                            Context context = v.getContext(); // 从长按的 TextView 拿上下文
                            Intent intent = new Intent(context, NoteEditActivity.class);
                            intent.putExtra("ai_note_content", chatMessage.getMessage());
                            context.startActivity(intent);
                        })
                        .setNegativeButton("取消", null)
                        .show();
                return true;
            });
        } else {
            // 渲染 Markdown 前，先确保 TextView 可见
            holder.userMessageTextView.setVisibility(View.GONE);
            holder.aiMessageTextView.setVisibility(View.VISIBLE);
            Markwon markwon = Markwon.create(holder.itemView.getContext());
            markwon.setMarkdown(holder.aiMessageTextView, chatMessage.getMessage());

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.aiMessageTextView.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_START);
            holder.aiMessageTextView.setLayoutParams(params);

            // 设置长按复制功能
            holder.aiMessageTextView.setOnLongClickListener(v -> {
                new AlertDialog.Builder(v.getContext())
                        .setTitle("保存回答")
                        .setMessage("是否将该回答保存在新建笔记？")
                        .setPositiveButton("是", (dialog, which) -> {
                            // 执行保存逻辑，比如跳转新建笔记页面并传入内容
                            Context context = v.getContext(); // 从长按的 TextView 拿上下文
                            Intent intent = new Intent(context, NoteEditActivity.class);
                            intent.putExtra("ai_note_content", chatMessage.getMessage());
                            context.startActivity(intent);

                        })
                        .setNegativeButton("否", null)
                        .show();
                return true;
            });

        }
    }

    @Override
    public int getItemCount() {
        return chatMessages.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView userMessageTextView;
        TextView aiMessageTextView;
        public ViewHolder(View itemView) {
            super(itemView);
            userMessageTextView = itemView.findViewById(R.id.userMessageTextView);
            aiMessageTextView = itemView.findViewById(R.id.aiMessageTextView);
        }
    }

    private void copyToClipboard(View view, String text) {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager)
                view.getContext().getSystemService(android.content.Context.CLIPBOARD_SERVICE);
        android.content.ClipData clip = android.content.ClipData.newPlainText("copied_text", text);
        clipboard.setPrimaryClip(clip);
        android.widget.Toast.makeText(view.getContext(), "已复制到剪贴板", android.widget.Toast.LENGTH_SHORT).show();
    }
}
